<?php

namespace Liuhelong\LaravelAdmin\Wechat\Http\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use \Liuhelong\LaravelAdmin\Wechat\Models\WechatOffiaccountQrcode;

class WechatOffiaccountQrcodeController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '事件二维码';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new WechatOffiaccountQrcode());

        $grid->column('id', __('Id'))->hide();
        $grid->column('title', __('laravel_admin_wechat.Qrcode title'));
        $grid->column('type', __('laravel_admin_wechat.Type'))->using([
			'forever' => '永久',
			'temporary' => '临时',
		], '未知')->dot([
			'forever' => 'primary',
			'temporary' => 'warning',
		], 'danger');
        $grid->column('key', __('laravel_admin_wechat.Key'));
        $grid->column('ticket', __('laravel_admin_wechat.Ticket'))->hide();
        $grid->column('url',__('laravel_admin_wechat.Url'))->qrcode();
        $grid->column('expired_at', __('laravel_admin_wechat.Expired at'));
        $grid->column('created_at', __('laravel_admin_wechat.Created at'));
        $grid->column('updated_at', __('laravel_admin_wechat.Updated at'))->hide();
		
		$grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableEdit();
			
        });
		
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(WechatOffiaccountQrcode::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('title', __('laravel_admin_wechat.Qrcode title'));
        $show->field('type', __('laravel_admin_wechat.Type'));
        $show->field('key', __('laravel_admin_wechat.Key'));
        $show->field('ticket', __('laravel_admin_wechat.Ticket'));
        $show->field('url', __('laravel_admin_wechat.Url'));
        $show->field('expired_at', __('laravel_admin_wechat.Expired at'));
        $show->field('created_at', __('laravel_admin_wechat.Created at'));
        $show->field('updated_at', __('laravel_admin_wechat.Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WechatOffiaccountQrcode());

        $form->text('title', __('laravel_admin_wechat.Qrcode title'))->rules('required');
        $form->radio('type', __('laravel_admin_wechat.Type'))->options([
			'forever' => '永久',
			'temporary' => '临时',
		])->when('temporary', function (Form $form) { 

			$form->datetime('expired_at', __('laravel_admin_wechat.Expired at'))->help(' 最多30天')->rules('required_if:type,temporary|date|after:now|before:'.now()->addDays(30));

		})->default('forever');
		
        $form->text('key', __('laravel_admin_wechat.Key'))->help(' 整数：临时二维码时为32位非0整型，永久二维码时最大值为100000；字符串：字符串类型，长度限制为1到64');
        
		/* 
		$form->fieldset('返回值', function (Form $form) {
			$form->text('ticket', __('Ticket'));
			$form->url('url', '二维码网址');
		});
         */

        return $form;
    }
}
